Skip to content

errors: don't print an empty line for Exit with empty message#2312

Open
alliasgher wants to merge 2 commits intourfave:mainfrom
alliasgher:fix-exit-empty-message-stderr
Open

errors: don't print an empty line for Exit with empty message#2312
alliasgher wants to merge 2 commits intourfave:mainfrom
alliasgher:fix-exit-empty-message-stderr

Conversation

@alliasgher
Copy link
Copy Markdown

Summary

When cli.Exit("", exitCode) is called, HandleExitCoder still wrote a bare newline to ErrWriter (usually stderr), because it called fmt.Fprintln(ErrWriter, err) unconditionally. An ExitCoder with an empty message should produce no output.

Before

cli.Exit("", 1)
// stderr: "\n"  ← unexpected blank line

After

cli.Exit("", 1)
// stderr: ""    ← nothing printed

Fix

Guard the fmt.Fprintln / fmt.Fprintf calls with a check that err.Error() != "" before writing to ErrWriter.

Fixes #2263

Checklist

  • Test added (TestHandleExitCoder_EmptyMessage)
  • Full test suite passes

When HandleExitCoder received an ExitCoder whose Error() returned an
empty string (e.g. cli.Exit("", code)), it still called
fmt.Fprintln(ErrWriter, err) which wrote a bare newline to stderr.

Guard the print with a check on the error message so that an empty
message produces no output at all.

Fixes urfave#2263

Signed-off-by: alliasgher <alliasgher123@gmail.com>
Adds TestHandleExitCoder_ErrorFormatterDirect to exercise the Fprintf
branch in HandleExitCoder when an ExitCoder also implements
ErrorFormatter and is passed in directly. The existing
TestHandleExitCoder_ErrorFormatter routes through a multiError which
goes down a different branch, so the direct path was not hit by any
test (codecov patch).

Signed-off-by: alliasgher <alliasgher123@gmail.com>
@alliasgher alliasgher force-pushed the fix-exit-empty-message-stderr branch from 6463da2 to 5f284b8 Compare April 15, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3.6.2 adds empty line to stderr when cli.Exit is called with empty message

1 participant